<template>
  <div class="login-container">
    <el-container>
      <el-header />
      <el-main>
        <el-form ref="ruleForm" :model="ruleForm" status-icon :rules="rules" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username" />
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="ruleForm.mobile" />
          </el-form-item>
          <el-form-item label="验证码" prop="code">
            <el-input v-model="ruleForm.code" />
            <el-button type="primary" @click="getcode">获取验证码</el-button>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" type="password" autocomplete="off" />
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input v-model="ruleForm.checkPass" type="password" autocomplete="off" />
          </el-form-item>
          <el-form-item label="用户类型" prop="apartmentOperatorType">
            <el-radio-group v-model="ruleForm.apartmentOperatorType">
              <el-radio label="1">个人职业房东</el-radio>
              <el-radio label="2">公司化公寓</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="tologin">提交</el-button>
            <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>

    <el-dialog title="分散式" :visible.sync="dialogFormVisible">
      <el-form ref="form" :model="form" :rules="rules2" class="demo-ruleForm">
        <el-form-item label="公司名称" prop="name" :label-width="formLabelWidth">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="负责人姓名" prop="legalPersonName" :label-width="formLabelWidth">
          <el-input v-model="form.legalPersonName" />
        </el-form-item>
        <el-form-item label="身份证号" prop="certNum" :label-width="formLabelWidth">
          <el-input v-model="form.certNum" />
        </el-form-item>
        <el-form-item label="身份证正面照" prop="certPicture" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1/api/sys/wish/business/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess1"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl1" :src="imageUrl1" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="身份证反面照" prop="certPicture1" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1/api/sys/wish/business/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess2"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl2" :src="imageUrl2" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="身份证手持照" prop="certPicture2" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1/api/sys/wish/business/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess3"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl3" :src="imageUrl3" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="城市地区" prop="province" :label-width="formLabelWidth">
          <el-select v-model="form.province" placeholder="省" @change="getCitys">
            <el-option
              v-for="item in provinces"
              :key="item.regionId"
              :label="item.regionName"
              :value="item.regionId"
            />
          </el-select>

          <el-select v-model="form.city" prop="city" placeholder="市" @change="getCountrys">
            <el-option
              v-for="item in citys"
              :key="item.regionId"
              :label="item.regionName"
              :value="item.regionId"
            />
          </el-select>

          <el-select v-model="form.county" prop="county" placeholder="区">
            <el-option
              v-for="item in countrys"
              :key="item.regionId"
              :label="item.regionName"
              :value="item.regionId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="address" :label-width="formLabelWidth">
          <el-input v-model="form.address" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm2('form')">立即创建</el-button>
          <el-button @click="resetForm2('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog title="集中式" :visible.sync="dialogTableVisible">
      <el-form ref="form" :model="form" :rules="rules2" class="demo-ruleForm">
        <el-form-item label="公司名称" prop="name" :label-width="formLabelWidth">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="负责人姓名" prop="legalPersonName" :label-width="formLabelWidth">
          <el-input v-model="form.legalPersonName" />
        </el-form-item>
        <el-form-item label="身份证号" prop="certNum" :label-width="formLabelWidth">
          <el-input v-model="form.certNum" />
        </el-form-item>
        <el-form-item label="身份证正面照" prop="certPicture" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1/api/sys/wish/business/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess1"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl1" :src="imageUrl1" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="身份证反面照" prop="certPicture1" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1/api/sys/wish/business/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess2"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl2" :src="imageUrl2" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="身份证手持照" prop="certPicture2" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1/api/sys/wish/business/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess3"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl3" :src="imageUrl3" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="营业执照" prop="url" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1/api/sys/wish/business/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess4"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl4" :src="imageUrl4" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="城市地区" prop="province" :label-width="formLabelWidth">
          <el-select v-model="form.province" placeholder="省" @change="getCitys">
            <el-option
              v-for="item in provinces"
              :key="item.regionId"
              :label="item.regionName"
              :value="item.regionId"
            />
          </el-select>

          <el-select v-model="form.city" prop="city" placeholder="市" @change="getCountrys">
            <el-option
              v-for="item in citys"
              :key="item.regionId"
              :label="item.regionName"
              :value="item.regionId"
            />
          </el-select>

          <el-select v-model="form.county" prop="county" placeholder="区">
            <el-option
              v-for="item in countrys"
              :key="item.regionId"
              :label="item.regionName"
              :value="item.regionId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="address" :label-width="formLabelWidth">
          <el-input v-model="form.address" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm2('form')">立即创建</el-button>
          <el-button @click="resetForm2('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      imageUrl1: '',
      imageUrl2: '',
      imageUrl3: '',
      imageUrl4: '',
      provinces: [],
      citys: [],
      countrys: [],
      rules2: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        legalPersonName: [
          { required: true, message: '请输入法人', trigger: 'blur' }
        ],
        certNum: [
          { required: true, message: '请输入身份证号', trigger: 'blur' }
        ],
        certPicture: [
          { required: true, message: '请上传正面身份证', trigger: 'blur' }
        ],
        certPicture2: [
          { required: true, message: '请上传反面身份证', trigger: 'blur' }
        ],
        certPicture3: [
          { required: true, message: '请上传手持身份证', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '请上传营业执照', trigger: 'blur' }
        ],
        province: [
          { required: true, message: '请选择省', trigger: 'blur' }
        ],
        city: [
          { required: true, message: '请选择市', trigger: 'blur' }
        ],
        county: [
          { required: true, message: '请选择区', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' }
        ]
      },
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        corporation: null,
        name: null,
        legalPersonName: null,
        certNum: null,
        certPicture: null,
        certPicture2: null,
        certPicture3: null,
        url: null,
        province: null,
        city: null,
        county: null,
        address: null,
        type: null

      },
      formLabelWidth: '120px',
      ruleForm: {
        username: '',
        password: null,
        mobile: '',
        apartmentOperatorType: null,
        code: null,
        type: null,
        checkPass: '',
        userId: null
      },
      rules: {
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ],
        apartmentOperatorType: [
          { required: true, message: '请选择商户类型', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.axios.get('http://127.0.0.1/api/sys/wish/sys-region/getArea?pid=' + 1).then((response) => {
      this.provinces = response.data.obj
    })
  },
  methods: {
    tologin() {

    },
    handleAvatarSuccess1(res, file) {
      this.form.certPicture = res
      this.imageUrl1 = res
    },
    handleAvatarSuccess2(res, file) {
      this.form.certPicture2 = res
      this.imageUrl2 = res
    },
    handleAvatarSuccess3(res, file) {
      this.form.certPicture3 = res
      this.imageUrl3 = res
    },
    handleAvatarSuccess4(res, file) {
      this.form.url = res
      this.imageUrl4 = res
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    getCitys() {
      this.citys = []
      this.axios.get('http://127.0.0.1/api/sys/wish/sys-region/getArea?pid=' + this.form.province).then((response) => {
        this.citys = response.data.obj
      })
    },
    getCountrys() {
      this.countrys = []
      this.axios.get('http://127.0.0.1/api/sys/wish/sys-region/getArea?pid=' + this.form.city).then((response) => {
        this.countrys = response.data.obj
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios.post('http://127.0.0.1/api/sys/wish/sys-login/reg', this.ruleForm).then((response) => {
            if (response.data.obj) {
              this.ruleForm.userId = response.data.obj
              if (this.ruleForm.apartmentOperatorType === 1) {
                this.dialogFormVisible = true
              } else {
                this.dialogTableVisible = true
              }
            } else {
              alert(response.data.msg)
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    getcode() {
      this.ruleForm.type = 1
      this.axios.get('http://127.0.0.1/api/sys/wish/sys-login/getCode', { params: this.ruleForm }).then((response) => {

      })
    },
    submitForm2(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogTableVisible = false
          this.dialogFormVisible = false
          this.form.type = this.ruleForm.apartmentOperatorType
          this.form.corporation = this.ruleForm.username
          this.form.userId = this.ruleForm.userId
          this.axios.post('http://127.0.0.1/api/sys/wish/business/add', this.form).then((response) => {

          })
        } else {
          return false
        }
      })
    },
    resetForm2(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

